﻿namespace BlackHole
{
    using System;
    using System.Web.UI.WebControls;
    using BlackHole.DAO;

    public partial class Workflow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RenderUI();
            }
        }

        protected void RenderUI()
        {
            // fill district list
            foreach (District d in Utility.GetAllDistricts())
            {
                comboDistricts.Items.Add(new ListItem(d.DistrictName, d.Id.ToString()));
            }

            // fill owner list of first distict id
            foreach (Owner o in Utility.GetOwnersByDistrictId(int.Parse(comboDistricts.Items[0].Value)))
            {
                comboOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }

            // fill device list
            comboDevices.Items.Add(new ListItem("(全部)", "0"));
            foreach (Device d in Utility.GetAllDevices())
            {
                comboDevices.Items.Add(new ListItem(d.DeviceName, d.Id.ToString()));
            }

            // fill product status
            foreach (Utility.ProductStatus s in Enum.GetValues(typeof(Utility.ProductStatus)))
            {
                radioStatus.Items.Add(new ListItem(s.ToString(), ((int)s).ToString()));
            }

            radioStatus.SelectedIndex = 1;

            // set default datasource
            dataProducts.AllowPaging = false;
            dataProducts.DataSourceID = sourceGetProductBySerial.ID;
            dataProducts.DataBind();

            // fill add new panel
            comboAddDevice.Items.Add(new ListItem(string.Empty, "0"));
            foreach (Device d in Utility.GetAllDevices())
            {
                comboAddDevice.Items.Add(new ListItem(d.DeviceName, d.Id.ToString()));
            }

            foreach (District d in Utility.GetAllDistricts())
            {
                comboAddDistricts.Items.Add(new ListItem(d.DistrictName, d.Id.ToString()));
            }

            txtSearchSerial.Focus();
        }

        protected void txtSearchSerial_TextChanged(object sender, EventArgs e)
        {
            txtSearchSerial.Text = txtSearchSerial.Text.Replace("S/N:", string.Empty);
            if (txtSearchSerial.Text.Length > 0)
            {
                // fix select all when search by serial
                radioStatus.SelectedIndex = 0;
                dataProducts.DataSourceID = sourceGetProductBySerial.ID;
                dataProducts.DataBind();
                dataProducts.SelectedIndex = 0;
                dataProducts_SelectedIndexChanged(sender, e);
            }
        }

        protected void comboDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboOwners.Items.Clear();
            comboOwners.Items.Add(new ListItem("(全部)", "0"));
            foreach (Owner o in Utility.GetOwnersByDistrictId(int.Parse(comboDistricts.SelectedValue)))
            {
                comboOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }

            dataProducts.DataSourceID = sourceGetProductByOwner.ID;
            dataProducts.DataBind();
        }

        protected void comboDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataProducts.DataSourceID = sourceGetProductByOwner.ID;
            dataProducts.DataBind();
        }

        protected void comboOwners_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataProducts.DataSourceID = sourceGetProductByOwner.ID;
            dataProducts.DataBind();
        }

        protected void radioStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dataProducts.DataSourceID == sourceGetProductByOwner.ID)
            {
                dataProducts.DataSourceID = sourceGetProductByOwner.ID;
            }
            else
            {
                dataProducts.DataSourceID = sourceGetProductBySerial.ID;
            }

            dataProducts.DataBind();
            HideAllPanel();
        }

        protected void dataProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            if (dataProducts.Rows.Count > 0)
            {
                txtSelectedDevice.Text = dataProducts.SelectedDataKey["ProductDeviceName"].ToString();
                txtSelectedPsamNO.Text = dataProducts.SelectedDataKey["PsamNO"].ToString();
                txtSelectedSerialNO.Text = dataProducts.SelectedDataKey["SerialNO"].ToString();
                lblSelectedOwner.Text =
                    dataProducts.SelectedDataKey["ProductDistrictName"].ToString() +
                    " " +
                    dataProducts.SelectedDataKey["ProductOwnerName"].ToString();
                Utility.ProductStatus status = (Utility.ProductStatus)int.Parse(dataProducts.SelectedDataKey["StatusId"].ToString());
                switch (status)
                {
                    case Utility.ProductStatus.全部:
                        break;
                    case Utility.ProductStatus.在库:
                        HideAllPanel();
                        btnOut.Visible = true;

                        break;
                    case Utility.ProductStatus.待修:
                        HideAllPanel();
                        panelProductRepair.Visible = true;
                        btnRepair.Enabled = false;
                        btnRepair.Visible = true;
                        break;
                    case Utility.ProductStatus.不在库:
                        HideAllPanel();
                        panelProductUnavailable.Visible = true;
                        btnIn.Visible = true;
                        chkCheckIn.Visible = true;
                        btnRequestRepair.Visible = false;
                        break;
                    default:
                        break;
                }

                // fill real and request faults list
                comboRequestFault.Items.Clear();
                comboRealFault.Items.Clear();
                chkCheckIn.Checked = false;

                comboRequestFault.Items.Add(new ListItem(string.Empty, "0"));
                comboRealFault.Items.Add(new ListItem(string.Empty, "0"));
                foreach (Fault f in Utility.GetFaultsByDeviceId(int.Parse(dataProducts.SelectedDataKey["ProductDeviceId"].ToString())))
                {
                    comboRequestFault.Items.Add(new ListItem(f.FaultCode + " " + f.FaultName, f.Id.ToString()));
                    comboRealFault.Items.Add(new ListItem(f.FaultCode + " " + f.FaultName, f.Id.ToString()));
                }
            }
            else
            {
                HideAllPanel();
                panelProductInformation.Visible = false;
                btnAddNew.Visible = true;
                panelProductAddNew.Visible = true;
                panelProductUnavailable.Visible = true;
                panelProductUnavailable.Enabled = false;
                txtAddSerialNO.Text = txtSearchSerial.Text;

                comboAddDevice.SelectedIndex = -1;
                comboAddDistricts.SelectedIndex = -1;
                txtRepairGear.Text = string.Empty;
                txtRepairMethod.Text = string.Empty;
            }
        }

        protected void HideAllPanel()
        {
            panelProductAddNew.Visible = false;
            panelProductRepair.Visible = false;
            panelProductUnavailable.Visible = false;

            btnAddNew.Visible = false;
            btnIn.Visible = false;
            btnIn.Enabled = false;
            chkCheckIn.Visible = false;
            btnOut.Visible = false;
            btnRepair.Visible = false;
            btnRequestRepair.Visible = false;
            btnAddAndRepair.Visible = false;
            btnAddAndRepair2.Visible = false;
            panelProductInformation.Visible = true;
        }

        protected void comboRequestFault_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 报修故障被选中
            if (int.Parse(comboRequestFault.SelectedValue) > 0)
            {
                // 新增产品
                if (btnAddNew.Visible == true)
                {
                    HideAllButton();
                    btnAddNew.Visible = false;
                    btnAddAndRepair.Visible = true;
                }
                else
                {
                    // 入库维修产品
                    HideAllButton();
                    btnIn.Visible = false;
                    chkCheckIn.Visible = false;
                    btnRequestRepair.Visible = true;
                }

                panelProductRepair.Visible = true;
            }
            else
            {
                if (btnAddAndRepair.Visible == true)
                {
                    HideAllButton();
                    btnAddAndRepair.Visible = false;
                    btnAddNew.Visible = true;
                }
                else
                {
                    HideAllButton();
                    btnRequestRepair.Visible = false;
                    btnIn.Visible = true;
                    chkCheckIn.Visible = true;
                }

                panelProductRepair.Visible = false;
            }
        }

        protected void HideAllButton()
        {
            btnAddAndRepair.Visible = false;
            btnAddAndRepair2.Visible = false;
            btnAddNew.Visible = false;
            btnIn.Visible = false;
            btnOut.Visible = false;
            btnRepair.Visible = false;
            btnRequestRepair.Visible = false;
        }

        protected void comboRealFault_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse(comboRealFault.SelectedValue) > 0)
            {
                // 新增产品
                if (btnAddAndRepair.Visible == true)
                {
                    btnAddAndRepair.Visible = false;
                    btnAddAndRepair2.Visible = true;
                }
                else
                {
                    // 入库的产品维修
                    btnRequestRepair.Visible = false;
                    btnRepair.Visible = true;
                    btnRepair.Enabled = true;
                }

                if (txtRepairGear.Text.Length == 0) txtRepairGear.Text = "无";
                if (txtRepairMethod.Text.Length == 0) txtRepairMethod.Text = "无";
            }
            else
            {
                if (btnAddAndRepair2.Visible == true)
                {
                    btnAddAndRepair2.Visible = false;
                    btnAddAndRepair.Visible = true;
                }
                else
                {
                    btnRepair.Visible = false;
                    btnRequestRepair.Visible = true;
                }
            }
        }

        protected void chkCheckIn_CheckedChanged(object sender, EventArgs e)
        {
            btnIn.Enabled = chkCheckIn.Checked;
        }

        protected void dataProducts_PageIndexChanged(object sender, EventArgs e)
        {
        }

        protected void dataProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dataProducts.PageIndex = e.NewPageIndex;
            dataProducts.DataSourceID = sourceGetProductByOwner.ID;
            dataProducts.DataBind();
        }

        protected void comboAddDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboAddOwners.Items.Clear();
            foreach (Owner o in Utility.GetOwnersByDistrictId(int.Parse(comboAddDistricts.SelectedValue)))
            {
                comboAddOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }

            panelProductUnavailable.Enabled = true;
        }

        protected void dataProducts_DataBinding(object sender, EventArgs e)
        {
            dataProducts.AllowPaging = true;
            dataProducts.SelectedIndex = -1;
        }

        protected void btnIn_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductCheckIn(int.Parse(dataProducts.SelectedDataKey["Id"].ToString()));
            lblError.Text =
                ret ? "入库成功" : "入库失败";
            HideAllPanel();
            dataProducts.DataBind();
        }

        protected void btnOut_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductCheckOut(int.Parse(dataProducts.SelectedDataKey["Id"].ToString()));
            lblError.Text =
                ret ? "出库成功" : "出库失败";
            HideAllPanel();
            dataProducts.DataBind();
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductAddNew(
                int.Parse(comboAddDevice.SelectedValue),
                int.Parse(comboAddOwners.SelectedValue),
                txtAddSerialNO.Text,
                txtAddPsamNO.Text,
                txtAddRomVersion.Text);
            lblError.Text =
                ret ? "新增成功" : "新增失败";
            HideAllPanel();
            dataProducts.DataBind();
        }

        protected void btnRequestRepair_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductRepair(
                    int.Parse(dataProducts.SelectedDataKey["Id"].ToString()),
                    int.Parse(comboRequestFault.SelectedValue),
                    int.Parse(comboRealFault.SelectedValue),
                    txtRepairMethod.Text,
                    txtRepairGear.Text);
            lblError.Text =
                ret ? "维修登记成功" : "维修登记失败";
            HideAllPanel();
            dataProducts.DataBind();
        }

        protected void btnRepair_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductRepair(
            int.Parse(dataProducts.SelectedDataKey["Id"].ToString()),
            int.Parse(comboRequestFault.SelectedValue),
            int.Parse(comboRealFault.SelectedValue),
            txtRepairMethod.Text,
            txtRepairGear.Text);
            lblError.Text =
                ret ? "维修成功" : "维修失败";
            HideAllPanel();
            dataProducts.DataBind();
        }

        protected void comboAddDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboRealFault.Items.Clear();
            comboRequestFault.Items.Clear();
            comboRequestFault.Items.Add(new ListItem(string.Empty, "0"));
            comboRealFault.Items.Add(new ListItem(string.Empty, "0"));
            foreach (Fault f in Utility.GetFaultsByDeviceId(int.Parse(comboAddDevice.SelectedValue)))
            {
                comboRequestFault.Items.Add(new ListItem(f.FaultCode + " " + f.FaultName, f.Id.ToString()));
                comboRealFault.Items.Add(new ListItem(f.FaultCode + " " + f.FaultName, f.Id.ToString()));
            }
        }

        protected void btnAddAndRepair_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductAddAndRepair(
                int.Parse(comboAddDevice.SelectedValue),
                int.Parse(comboAddOwners.SelectedValue),
                txtAddSerialNO.Text,
                txtAddPsamNO.Text,
                txtAddRomVersion.Text,
                int.Parse(comboRequestFault.SelectedValue),
                0,
                txtRepairMethod.Text,
                txtRepairGear.Text);

            lblError.Text =
                ret ? "新增并维修登记成功" : "新增并维修登记失败";
            HideAllPanel();
            dataProducts.DataBind();
        }

        protected void btnAddAndRepair2_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductAddAndRepair(
                int.Parse(comboAddDevice.SelectedValue),
                int.Parse(comboAddOwners.SelectedValue),
                txtAddSerialNO.Text,
                txtAddPsamNO.Text,
                txtAddRomVersion.Text,
                int.Parse(comboRequestFault.SelectedValue),
                int.Parse(comboRealFault.SelectedValue),
                txtRepairMethod.Text,
                txtRepairGear.Text);

            lblError.Text =
                ret ? "新增并维修完成" : "新增并维修失败";
            HideAllPanel();
            dataProducts.DataBind();
        }
    }
}